<?php
class User
{			
	var $use_code;
	var $use_name;
	var $use_lastname;
	var $use_email;
	var $use_login;
	var $use_status;
	var $use_type;
	var $use_date_create;
	
	/**
	*	Constructor
	*	@autor: JJR	
	*	@date: 01/25/2012
	*/	
	function User (){
		$use_code = 0;
		$use_name = '';
		$use_lastname = '';
		$use_email = '';
		$use_login = '';
		$use_status = '';
		$use_type = '';	
		$use_date_create = '';
	}
	
	/**
	*	@autor: JJR
	*	@date: 01/25/2012
	*	Funcion encargada de adicionar un nuevo usuario al sistema.
	*	@return:
	*/
	function addUser($data){
		$sql = "
		 INSERT INTO  user (
		`use_code` ,
		`use_name` ,
		`use_lastname` ,
		`use_email` ,
		`use_login` ,
		`use_password` ,
		`use_status` ,
		`use_type`,
		`use_date_create`
		)
		VALUES (
		NULL ,  
		'".$data->use_name."',  
		'".$data->use_lastname."',  
		'".$data->use_email."',  
		'".$data->use_login."',  
		'".$data->use_password."',  
		'Active',  
		'".$data->use_type."',  
		'".date("Y-m-d")."');";	
		
		if(mysql_query($sql)){		
			return mysql_insert_id();
		}else
			return false;
	}
		
	/**
	*	Funcion encargada de verificar que la cuenta insertada no exista
	*	@autor: JJR
	*	@date: 01/25/2012
	*	@parameters: 
	*	@return:
	*/	
	public function userExist($use_login, $use_code = 0){
		$where = '';
		if( $use_code != 0 )
			$where = ' AND u.use_code <> '.$use_code;
			
		$consulta = mysql_query("SELECT u.use_code FROM user u WHERE u.use_login = '".$use_login."' ".$where);
			
		if( $row = mysql_fetch_array ( $consulta ) ){
			$this->use_code = $row['use_code'];
			return true;
		}
		else
			return false;
	}
	
	/**
	*	
	*	@autor: JJR
	*	@date: 01/25/2012
	*	@parameters: Esta funcion es la encargada de listar los usuarios que existen en el sitio.
	*	@return:
	*/

	function userList($search='',$order_by='', $inicio = 0,$cantidad = 0){
		$where = '';		
		if($search != '')
		$where = " AND (use_name like '%".$search."%'
				   OR use_lastname like '%".$search."%' 		           
				   OR use_email like '%".$search."%' 
				   OR use_login like '%".$search."%')";
		
		if ($order_by == '')
			$order_by = ' ORDER BY use_name ';
		
		$sql = "SELECT u.use_code, u.use_name, u.use_lastname, u.use_email, u.use_login, u.use_status, u.use_type FROM user u 
				WHERE u.use_status <> 'Deleted' ".$where." ".$order_by;  
		
		if ( !($inicio == 0 && $cantidad == 0 ) )
			$sql .= ' LIMIT '.$inicio.', '.$cantidad.';';
		
		$consult  = mysql_query($sql);
		$data;
		$i = 0;
		while($row = mysql_fetch_array($consult)){
			$data[$i]->use_code = $row['use_code'];
			$data[$i]->use_name = $row['use_name'];
			$data[$i]->use_lastname = $row['use_lastname'];
			$data[$i]->use_email = $row['use_email'];
			$data[$i]->use_login = $row['use_login'];
			$data[$i]->use_status = $row['use_status'];	
			$data[$i]->use_type = $row['use_type'];
			$data[$i]->telephones = $this->getTelephones((int) $row['use_code']);
			$i++;				
		}
		return $data;
	}		
	
	/**
	*	Funcion encargada de contar la cantidad de usuarios en la aplicacion.
	*	@autor: JJR
	*	@date: 01/25/2012
	*	@parameters: 
	*	@return:
	*/
	function countUser($search=''){		
		$where = '';		
		if($search != '')
		$where = " AND (use_name like '%".$search."%'
				   OR use_lastname like '%".$search."%' 		           
				   OR use_email like '%".$search."%' 
				   OR use_login like '%".$search."%')";
				
		$sql = "SELECT COUNT(*) as cant FROM user WHERE `use_status`<> 'Deleted' ".$where." ";
	
		$consulta = mysql_query($sql);
		$row = mysql_fetch_array($consulta);
			
		return $row['cant'];
	}
		
	/**
	*	Funcion encargada de cambiar el estado de un cliente
	*	@autor: JSL
	*	@date: 01/27/2012
	*	@parameters: $use_code: codigo del usuario, $use_status: nuevo estado para el usuario.
	*	@return: true si cambio el estado del cliente con exito. false en caso contrario
	*/
	function userChangeStatus( $use_code , $use_status){
		$sql = "UPDATE user SET use_status='".$use_status."' WHERE use_code=".$use_code.";";
		return mysql_query ($sql);
	}
	
	/**
	*	Funcion encargada de validar una cuenta.
	*	@autor: JSL
	*	@date: 27/01/2011
	*/
	function validateAccount($use_login, $use_password){
		$sql = "SELECT u.use_code, u.use_name, u.use_lastname, u.use_email, u.use_login, u.use_status, u.use_type, u.use_date_create FROM user u 
				WHERE u.use_login='".$use_login."' AND u.use_password='".$use_password."'";		
		
		if ( $result =  mysql_query ($sql) ){
			if( $row =  mysql_fetch_array ($result) ){				
				$this->use_code = $row['use_code'];
				$this->use_name = $row['use_name'];
				$this->use_lastname = $row['use_lastname'];
				$this->use_email = $row['use_email'];
				$this->use_login = $row['use_login'];
				$this->use_status = $row['use_status'];		
				$this->use_type = $row['use_type'];	
				$this->use_date_create = $row['use_date_create'];
				return true;
			}
			else
				return false;
		}
		else
			return false;
	}
	
	/**
	*	Funcion encargada de conseguir los datos de un usuario especifico para editarlo.
	*	@autor: JJR
	*	@date: 01/26/2012
	*	@parameters: 
	*	@return:
	*/
	function getUserData($use_code){
		$sql = "SELECT u.use_code, u.use_name, u.use_lastname, u.use_email, u.use_login, u.use_password, u.use_type FROM user u WHERE use_code = ".$use_code;
		$consult  = mysql_query($sql);		
		$row = mysql_fetch_array($consult);
		
		$data->use_code = $row['use_code'];
		$data->use_name = $row['use_name'];
		$data->use_lastname = $row['use_lastname'];
		$data->use_email = $row['use_email'];
		$data->use_login = $row['use_login'];
		$data->use_password = $row['use_password'];
		$data->use_type = $row['use_type'];

		return $data;

	}
	

	/**
	*	Funcion encargada de Actualizar los datos de un usuario.
	*	@autor: JJR
	*	@date: 01/26/2012
	*	@parameters: 
	*	@return:
	*/	
	function editUser($new_data){
	
		$sql="UPDATE `user` SET 
		`use_name`     = '".$new_data->use_name."',
		`use_lastname`    = '".$new_data->use_lastname."',
		`use_email`    = '".$new_data->use_email."',
		`use_login`    = '".$new_data->use_login."',
		`use_password` = '".$new_data->use_password."',
		`use_type`     = '".$new_data->use_type."'
		WHERE `user`.`use_code` =".$new_data->edit_code.";"; 
			
		return mysql_query($sql);
	}

	/**
	*	Funcion encargada de Actualizar los datos de un usuario.
	*	@autor: JJR
	*	@date: 01/27/2012
	*	@parameters: 
	*	@return:
	*/	
	function deleteUser($use_code){
		$sql="UPDATE `user` SET `use_status` =  'Deleted' WHERE `use_code` =".$use_code;
		return mysql_query($sql);
	}
	
	/**
	*	Funcion respponsable de agregar un telefono para un usuario;
	*	@autor: JSL
	*	@date: 29/01/2012
	*/
	function addTelephone($data){
		$sql = "INSERT INTO telephone (tel_code, tel_number, tel_ext, tel_type, id_fk) VALUES (NULL, '".$data->tel_number."', '".$data->tel_ext."', 'User', '".$data->id_fk."');"; 
		if( mysql_query ($sql) ) 
			return true;	
		return false;
	}
	
	/**
	*	Funcion responsable de listar el/los telefonos de un usuario.
	*	@autor: JSL
	*	@date: 30/01/2012
	*/
	function getTelephones($id_fk){
		$sql = "SELECT t.tel_code, t.tel_number, t.tel_ext FROM telephone t WHERE t.tel_type='User' AND t.id_fk=".$id_fk." ORDER BY tel_code;";
		$data = array ();
		if ( $result = mysql_query ($sql) ){
			$i = 0;
			while ( $row = mysql_fetch_array($result) ){
				$data[$i]->tel_code = $row["tel_code"];
				$data[$i]->tel_number = $row["tel_number"];
				$data[$i]->tel_ext = $row["tel_ext"];
				
				$i++;
			}
		}
				
		return $data;
	}
	
	/**
	*	Funcion responsable eliminar los telefonos de un usuario.
	*	@autor: JSL
	*	@date: 30/01/2012
	*/
	function deleteTelephones($id_fk){
		$sql = "DELETE FROM telephone  WHERE tel_type='User' AND id_fk=".$id_fk.";";
		return mysql_query ($sql);
	}
	
	/**
	*	Funcion responsable de listar los adviser de la aplicacion.
	*	@autor: JSL
	*	@date: 31/01/2012
	*/
	function listAdvisers(){
		$sql = "SELECT u.use_code, u.use_name, u.use_lastname FROM user u WHERE u.use_status='Active';";
				
		if ( $result = mysql_query ($sql) ){
			$i =  0;
			while ( $row = mysql_fetch_array ($result) ){
				$data[$i]->use_code = $row["use_code"];
				$data[$i]->use_name = $row["use_name"];
				$data[$i]->use_lastname = $row["use_lastname"];
				
				$i++;
			}			
			return $data;
		}
		else
			return false;	
	}
}
?>